(function (G) {
    G.vue({
        data: {
            types:[],
            facility:{},
            scene:'',
            records:[],
            models:{},
            APIURL:G.Storage.get('APIURL')
        },
        methods: {
            selectFaclity(){
                G.Storage.set('selectFaclity',item => {
                    G.Storage.remove('selectFaclity')
                    G.put('/api/model/facility',{ id:item.id,scene:this.data.scene }).then(res => {
                        this.getFacility(this.data.scene)
                    })
                })
                G.$go(`/pages/facility/list/list?name=资源设施&type=all`)
            },
            getFacility(scene){
                G.get('/api/model/facility',{ scene, sql:'defaultlFacility'}).then(facilitys => {
                    if(facilitys.length){
                        this.setData({
                            facility:facilitys[0]
                        })
                        this.getRecords()
                    }
                })
            },
            getRecords(){
                G.get('/api/model/facilityRecord',{sql:'defaultlFacilityRecord',facilityId:this.data.facility.id}).then(res => {
                    for(let i = 0; i < res.length; i ++){
                        if(res[i].images) res[i].images = res[i].images.split(',')
                    }
                    this.setData({
                        records:res
                    })
                })
            },
            edit(event){
                let index = event.currentTarget.dataset.index
                var modeleditquery = {
                    values: index == -1 ? {} : this.data.records[index],
                    title: index == -1 ? '新增保养记录' : '修改保养记录',
                    meth: index == -1 ? 'POST' : 'PUT',
                    refresh: (next,data)=>{//后修改
                        this.getRecords()
                    },
                    models: this.data.models,
                    url: '/api/model/facilityRecord'
                }
                if(index == -1){
                    modeleditquery.values.userId = G.Storage.get('userInfo').userId
                    modeleditquery.values.facilityId = this.data.facility.id
                    G.Storage.set('modeleditquery', modeleditquery)
                    G.$go('/pages/model/edit/edit')
                }else{
                    G.confirm('确定删除？').then(res => {
                        G.delete('/api/model/facilityRecord',{id:modeleditquery.values.id,sql:{real:true}}).then(res => {
                            this.getRecords()
                        })
                    })
                }
            }
        },
        mounted(query) {
            let scene = query.scene
            this.setData({
                scene
            })
            if(scene) this.getFacility(scene)
            G.get('/model/facilityRecord').then(models => {
                models.facilityId = null
                models.userId = null
                this.setData({
                    models
                })
            })
            G.get('/model/facility',{}).then(res => {
                G.Storage.set('modelFacility',res)
                let types = {}
                for(let i = 0; i < res.type.attr.actions.length; i ++){
                    types[res.type.attr.actions[i].value] = res.type.attr.actions[i].name
                }
                this.setData({
                    types
                })
            })
        }
    })
})(wx.$)